package edu.calpoly.android.echoes;

import android.graphics.RectF;



public class MyPhysics {
	public static final float GRAVITY = (float) .03;
	public static final float SPRING = (float) .05;
	public static final float TRAVEL_SPEED = (float) 3.5;
	public static final int BORDER_PADDING = 50;
	
	
	public static boolean checkCollision(MyCircle circle, MyRectangle rectangle) {
		float circleDistX = Math.abs(circle.getX() - rectangle.inX);
		float circleDistY = Math.abs(circle.getY() - rectangle.inY);
		float r_width = rectangle.right - rectangle.left;
		float r_height = rectangle.bottom - rectangle.top;
		
		
		//the circle is too far from the rectangle
		if(circleDistX > (r_width/2 + circle.getR()) ||
		   circleDistY > (r_height/2 + circle.getR())) {
			return false;
		}
		
		//the circle intersects one of the rectangle's sides
		if(circleDistX <= (r_width/2)) {
			circle.direction.setY(circle.direction.getY()*-1);
			return true;
		}
		if(circleDistY <= (r_height/2)) {
			circle.direction.setX(circle.direction.getX()*-1);
			return true;
		}
		
		float cornerCheck = (circleDistX - r_width/2)*(circleDistX - r_width/2) +
			(circleDistY - r_height/2)*(circleDistY - r_height/2);
		
		//the circle intersects the very corner of the rectangle
		if(cornerCheck <= (circle.getR()*circle.getR())) {
			circle.direction.setX(circle.direction.getX()*-1);
			circle.direction.setY(circle.direction.getY()*-1);
		}
		
		return (cornerCheck <= (circle.getR()*circle.getR()));
	}
	
	public static boolean checkCollision(MyCircle circle, MyRectangle rectangle, int flag) {
		float circleDistX = Math.abs(circle.getX() - rectangle.inX);
		float circleDistY = Math.abs(circle.getY() - rectangle.inY);
		float r_width = rectangle.right - rectangle.left;
		float r_height = rectangle.bottom - rectangle.top;
		
		
		if(circleDistX > (r_width/2 + circle.getR()) ||
		   circleDistY > (r_height/2 + circle.getR())) {
			return false;
		}
		
		if(circleDistX <= (r_width/2))
			return true;
			
		if(circleDistY <= (r_height/2))
			return true;
		
		float cornerCheck = (circleDistX - r_width/2)*(circleDistX - r_width/2) +
			(circleDistY - r_height/2)*(circleDistY - r_height/2);
		
		
		return (cornerCheck <= (circle.getR()*circle.getR()));
	}

	public static boolean checkCollision(MyRectangle r1, MyRectangle r2) {
		RectF rect1 = new RectF(r1.left, r1.top, r1.right, r1.bottom);
		
		return (rect1.contains(r2.left,r2.top) || rect1.contains(r2.right,r2.top) ||
				rect1.contains(r2.right,r2.bottom)|| rect1.contains(r2.left,r2.bottom)); 
	}
}
